package com.coupon.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;

import com.coupon.bl.Constant;
import com.coupon.bl.CouponException;
import com.coupon.dal.entity.Coupon;
import com.coupon.dal.entity.User;
import com.coupon.dao.AbstractDataAccessObject;
import com.coupon.dao.ifc.CouponDaoIfc;
import com.coupon.dao.ifc.UserDaoIfc;



public class CouponDaoImpl extends AbstractDataAccessObject<Coupon> implements Constant,CouponDaoIfc<Coupon> {

	
	public CouponDaoImpl() {
		super();
	}

	public void save(Coupon couponEntity) throws CouponException {
		getEntityManager().persist(couponEntity);
	}
	
	
	
	public Coupon findbyPK(String key) throws CouponException{
		Coupon couponEntity = null;
		couponEntity= getEntityManager().find(Coupon.class, key);
		if(couponEntity!=null ){
			return couponEntity;
		}else{
			throw new CouponException(Constant.NO_USER_FOUND);
		}
	}
	
	@SuppressWarnings("unchecked")
	public Coupon findByBusinessKeys(Serializable... businessKeys) {
		assert businessKeys != null;
		assert businessKeys.length == 1;
		Query namedQuery = getEntityManager().createNamedQuery("Coupon.findByEmail");
		namedQuery.setParameter("email", (String) businessKeys[0]);
		List<Coupon> resultList = namedQuery.getResultList();
		if (resultList.isEmpty()) {
			return null;
		} else {
			return (Coupon) resultList.get(0);
		}
	}
	
	public void updateUser(Coupon couponEntity) throws CouponException{
		/*Coupon  couponDB = findbyPK(couponEntity.getEmail());
		if(couponDB==null){
			throw new CouponException (Constant.NO_USER_FOUND);
		}

		couponDB.setPhone(couponEntity.getPhone());
		couponDB.setEmail(couponEntity.getEmail());
		getEntityManager().merge(couponDB);*/
	}

	@Override
	public void updateCoupon(Coupon couponEntity) throws CouponException {
		// TODO Auto-generated method stub
		
	}

}




